package cn.offcn.controller;

import cn.offcn.entity.Employee;
import cn.offcn.entity.Sources;
import cn.offcn.service.SourcesService;
import cn.offcn.utils.OAResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yuliantao
 * @create 2020 -09 -2020-09-17 22:05
 * @description 功能描述
 */
@Controller
@RequestMapping("/sources")
public class SourcesController {

    @Autowired
    private SourcesService sourcesService;

    @ResponseBody
    @RequestMapping("/getRootSourcesByPid")
    public List<Sources> getRootSourcesByPid(@RequestParam(name = "pid",defaultValue = "0") int pid){
        return sourcesService.getRootSourcesByPid(pid);
    }

    @ResponseBody
    @RequestMapping("/getAllParentNode")
    public List<Sources> getAllParentNode(@RequestParam(name = "pid",defaultValue = "0") int pid){

        List<Sources> sourcesList=new ArrayList<>();
        return sourcesService.getParentSources(pid,sourcesList);
    }

    @ResponseBody
    @RequestMapping("/addSources")
    public OAResult addSources(Sources sources){

        return sourcesService.saveSources(sources);
    }

    @ResponseBody
    @RequestMapping("/deleteSourcesById")
    public OAResult deleteSourcesById(int id){

        return sourcesService.deleatSourceById(id);
    }

    @ResponseBody
    @RequestMapping("/getSourcesById")
    public Sources getSourcesById(int id) {
        return sourcesService.getSourcesById(id);
    }

    @ResponseBody
    @RequestMapping("/updateSourcesById")
    public OAResult updateSourcesById(Sources sources){

        return sourcesService.updeteSourcesById(sources);
    }
    @ResponseBody
    @RequestMapping("/querySourcesByEidAndPid")
    public List<Sources> querySourcesByEidAndPid(@RequestParam(name = "pid",defaultValue = "1") int pid, HttpSession session){

        Employee employee = (Employee)session.getAttribute("activeEmployee");
        return sourcesService.querySourcesByEidAndPid(employee.getEid(),pid);
    }
}
